package com.cn.xact.ty_treasured_book.activity;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.cn.xact.ty_treasured_book.R;
import com.cn.xact.ty_treasured_book.activity.MsgListActivity.MyNetworkCallBack;
import com.cn.xact.ty_treasured_book.net.NetworkCallBack;
import com.cn.xact.ty_treasured_book.net.NetworkImpl;
import com.cn.xact.ty_treasured_book.util.UIHelper;

import android.os.Bundle;
import android.app.Activity;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Color;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.View.OnClickListener;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

public class HomeBillBoardActivity extends Activity {

    private ListView mBillboardList;
    private BillboardListViewAdapter mBillboardListViewAdapter;
    private NetworkImpl mRankNetwork;

    private String[] mTitles = null;
    private String[] mCredit = null;
    private Bitmap[] mIcons = null;
    private String mCostPre = null;

    private List<Map<String, Object>> mListItems;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);
        setContentView(R.layout.activity_home_bill_board);
        getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, R.layout.common_customer_title);

        ImageView tmp = (ImageView)findViewById(R.id.common_title_indicator);
        tmp.setImageResource(R.drawable.main_home);
        TextView title1 = (TextView)findViewById(R.id.common_title_first_section);
        title1.setText(R.string.title_activity_home);
        TextView separator1 = (TextView)findViewById(R.id.common_title_first_separator);
        separator1.setVisibility(View.VISIBLE);
        TextView title2 = (TextView)findViewById(R.id.common_title_second_section);
        title2.setText(R.string.home_user_billboard);
        title2.setVisibility(View.VISIBLE);

        title1.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View arg0) {
                HomeBillBoardActivity.this.finish();
            }

        });

        mRankNetwork = new NetworkImpl(this, new MyNetworkCallBack());
        mRankNetwork.getRankAsync(123);

        mBillboardList = (ListView)findViewById(R.id.home_bill_board_list);
        mListItems = getListItems();
        mBillboardListViewAdapter = new BillboardListViewAdapter(this, mListItems);
        mBillboardList.setAdapter(mBillboardListViewAdapter);

        mCostPre = getResources().getString(R.string.home_user_credit_rank_cost);
    }

    private List<Map<String, Object>> getListItems() {
        List<Map<String, Object>> innnerListItems = new ArrayList<Map<String, Object>>();
        if (mTitles != null) {
            for(int i = 0; i < mTitles.length; i++) {
                Map<String, Object> map = new HashMap<String, Object>();
                map.put("title", mTitles[i]);
                map.put("credit", mCredit[i]);
                map.put("icon", mIcons[i]);
                innnerListItems.add(map);
            }
        }
        return innnerListItems;
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.home_bill_board, menu);
        return true;
    }

    private class BillboardListViewAdapter extends BaseAdapter {

        private List<Map<String, Object>> listItems;
        private LayoutInflater listContainer;

        public final class ListItemView{
            public Button light;
            public TextView title;
            public TextView credit;
            public ImageView icon;
     }

        public BillboardListViewAdapter(Context context, List<Map<String, Object>> listItems) {
            listContainer = LayoutInflater.from(context);
            this.listItems = listItems;
        }

        @Override
        public int getCount() {
            // TODO Auto-generated method stub
            return listItems.size();
        }

        @Override
        public Object getItem(int arg0) {
            // TODO Auto-generated method stub
            return listItems.get(arg0);
        }

        @Override
        public long getItemId(int arg0) {
            // TODO Auto-generated method stub
            return 0;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {

            ListItemView  listItemView = null;
            if (convertView == null) {
                listItemView = new ListItemView();
                convertView = listContainer.inflate(R.layout.home_bill_board_item, null);

                listItemView.light = (Button)convertView.findViewById(R.id.home_bill_board_item_light);
                listItemView.title = (TextView)convertView.findViewById(R.id.home_bill_board_item_name);
                listItemView.credit = (TextView)convertView.findViewById(R.id.home_bill_board_item_credit);
                listItemView.icon = (ImageView)convertView.findViewById(R.id.home_bill_board_item_icon);
                convertView.setTag(listItemView);
            }else {
                listItemView = (ListItemView)convertView.getTag();
            }

            listItemView.title.setText((String)listItems.get(position).get("title"));
            listItemView.credit.setText(mCostPre + (String)listItems.get(position).get("credit"));
            listItemView.icon.setImageBitmap((Bitmap)listItems.get(position).get("icon"));

            if (position < 3) {
                listItemView.light.setBackgroundResource(R.drawable.rank_top3_button);
            } else {
                listItemView.light.setBackgroundResource(R.drawable.rank_button);
            }

            int rank = position + 1;
            listItemView.light.setText("No." + rank);
            listItemView.light.setTextColor(Color.WHITE);
            listItemView.light.setTextSize(20);

            return convertView;
        }

    }

    class MyNetworkCallBack extends NetworkCallBack {
        @SuppressWarnings("unchecked")
        public void onNetworkCallBack(int id, boolean success, Object attach1, Object attach2) {
            if (!success) {
                UIHelper.showConfirmDialog(HomeBillBoardActivity.this, null, getResources().getString(R.string.server_busy));
                return;
            }
            switch(id) {
            case NetworkImpl.NET_WORK_ID_GET_RANK:
                mListItems = (List<Map<String, Object>>)attach1;
                mBillboardListViewAdapter = new BillboardListViewAdapter(HomeBillBoardActivity.this, mListItems);
                mBillboardList.setAdapter(mBillboardListViewAdapter);
                break;
            }
        }
    }

}
